package com.example.system_backend.controller;

import com.example.system_backend.dto.NewTreatDTO;
import com.example.system_backend.dto.TeethTreatDTO;
import com.example.system_backend.entity.Teeth;
import com.example.system_backend.entity.Treatment;
import com.example.system_backend.repository.UserRepository;
import com.example.system_backend.service.TreatmentService;
import com.example.system_backend.util.ResultVOUtil;
import com.example.system_backend.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/treatment")
public class TreatmentHandler {

    @Autowired
    private TreatmentService treatmentService;
    @Autowired
    private UserRepository userRepository;

    @PostMapping("/create")
    public ResultVO createTreatment(@RequestBody NewTreatDTO newTreatDTO) {
        String name = newTreatDTO.getName();
        String patient_email = newTreatDTO.getUser_email();
        String dentist_email = newTreatDTO.getDt_email();

        Integer result = treatmentService.creatTreatment(name, patient_email, dentist_email);

        if (result == 0) {
            return ResultVOUtil.success(null, "添加成功");
        } else {
            return ResultVOUtil.error("病人用户不存在");
        }
    }

    @GetMapping("/treatmentByDentist")
    public ResultVO treatmentByDentist(@RequestParam(value = "email") String dtEmail) {
        List<DtTreatmentVO> voList = new ArrayList<>();
        List<Treatment> treatmentList = treatmentService.treatmentByDt(dtEmail);
        for (Treatment treatment: treatmentList) {
            DtTreatmentVO dtTreatmentVO = new DtTreatmentVO();
            dtTreatmentVO.setTitle(treatment.getName());
            dtTreatmentVO.setUser_email(treatment.getUserEmail());
            dtTreatmentVO.setUser_name(userRepository.findByEmail(treatment.getUserEmail()).getName());
            List<TeethVO> list = new ArrayList<>();
            List<Teeth> teethList = treatmentService.teethByTreat(treatment.getId());
            for (Teeth teeth: teethList) {
                TeethVO teethVO = new TeethVO();
                teethVO.setId(teeth.getId());
                teethVO.setName(teeth.getName());
                teethVO.setTime(teeth.getTime());
                int type = teeth.getType();
                if (type == 1) {
                    teethVO.setType("上牙");
                } else {
                    teethVO.setType("下牙");
                }
                list.add(teethVO);
            }
            dtTreatmentVO.setTeeths(list);
            voList.add(dtTreatmentVO);
        }
        return ResultVOUtil.success(voList);
    }

    @GetMapping("/treatmentByUser")
    public ResultVO treatmentByUser(@RequestParam(value = "email") String userEmail) {
        List<TreatmentVO> voList = new ArrayList<>();
        List<Treatment> treatmentList = treatmentService.treatmentByUser(userEmail);
        for (Treatment treatment: treatmentList) {
            TreatmentVO treatmentVO = new TreatmentVO();
            treatmentVO.setTitle(treatment.getName());
            List<TeethVO> list = new ArrayList<>();
            List<Teeth> teethList = treatmentService.teethByTreat(treatment.getId());
            for (Teeth teeth: teethList) {
                TeethVO teethVO = new TeethVO();
                teethVO.setId(teeth.getId());
                teethVO.setName(teeth.getName());
                teethVO.setTime(teeth.getTime());
                int type = teeth.getType();
                if (type == 1) {
                    teethVO.setType("上牙");
                } else {
                    teethVO.setType("下牙");
                }
                list.add(teethVO);
            }
            treatmentVO.setTeeths(list);
            voList.add(treatmentVO);
        }
        return ResultVOUtil.success(voList);
    }

    @GetMapping("/treatNamesByDentist")
    public ResultVO treatNamesByDentist(@RequestParam(value = "email") String dtEmail) {
        List<TreatNameVO> voList = new ArrayList<>();
        List<Treatment> treatmentList = treatmentService.treatmentByDt(dtEmail);
        for (Treatment treatment: treatmentList) {
            TreatNameVO treatNameVO = new TreatNameVO();
            treatNameVO.setLabel(treatment.getName());
            treatNameVO.setValue(treatment.getId());
            voList.add(treatNameVO);
        }
        return ResultVOUtil.success(voList);
    }

    @PostMapping("/adding")
    public ResultVO add2Treat(@RequestBody TeethTreatDTO teethTreatDTO) {
        Integer teethId = teethTreatDTO.getTeethId();
        Integer treatId = teethTreatDTO.getTreatId();
        Integer result = treatmentService.add2Treatment(teethId, treatId);
        if (result == 0) {
            return ResultVOUtil.success(null, "添加成功");
        } else {
            return ResultVOUtil.error("该牙齿已在该疗程中");
        }
    }
}
